﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DeepZoomPivotConstructor.ErrorAndLoggingInterfaces;
using System.IO;

namespace PivotPhysicsPlotsGenerator
{
    /// <summary>
    /// Log to an output file. Check point so that, even if we crash, we don't loose much.
    /// </summary>
    class FileLogger : IErrorLogger
    {
        private FileInfo _output;
        private StreamWriter _writer;

        public FileLogger(FileInfo outputFile)
        {
            _output = outputFile;

            _writer = new StreamWriter(_output.Open(FileMode.Append, FileAccess.Write, FileShare.Read));
        }

        public void LogMessage(string ID, string message)
        {
            LogMessage(ID, message, null);
        }

        /// <summary>
        /// Write the guy out.
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="message"></param>
        /// <param name="e"></param>
        public void LogMessage(string ID, string message, Exception e)
        {
            _writer.Write(DateTime.Now.ToString());
            _writer.Write(" - " + ID + " - " + message);
            if (e != null)
            {
                _writer.WriteLine(" ERROR: " + e.Message + " at " + e.StackTrace);
                _writer.Flush();
                if (e.InnerException != null)
                {
                    LogMessage(ID, message + " -- inner exception of the above error...", e.InnerException);
                }
            }
            else
            {
                _writer.WriteLine();
            }
            _writer.Flush();
        }

        public void SetLogfileLocation(System.IO.DirectoryInfo newDirectory)
        {
            throw new NotImplementedException();
        }
    }
}
